/**
 * vim: tabstop=4
 * 
 * @license		http://www.gnu.org/licenses/gpl.html GPL Version 3
 * @author		Ian Moore <imooreyahoo@gmail.com>
 * @copyright	Copyright (c) 2011 Ian Moore
 * @version		$Id: ProcessList.js 77 2011-12-12 01:41:56Z imooreyahoo@gmail.com $
 *
 * This file is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file. If not, see <http://www.gnu.org/licenses/>.
 */
// require("js/omv/NavigationPanel.js")
// require("js/omv/DiagPanel.js")
// require("js/omv/module/admin/Processes.js")

Ext.ns("OMV.Module.Diagnostics");

/**
 * 
 * DHCP leases grid
 */
OMV.Module.Diagnostics.ProcessList = function(config) {

	var initialConfig = {
		id: 'OMV.Module.Diagnostics.ProcessList',
		hideAdd: true,
		hideEdit: true,
		hideDelete: true,
		hideRefresh: false,
		hidePagingToolbar: true,
		loadMask: true,
		stateful: false,
		autoExpandColumn: 'COMMAND',
		colModel: new Ext.grid.ColumnModel({
			columns: [{
				header: "PID",
				sortable: true,
				dataIndex: "PID",
				id: "PID",
				width: 5
			},{
				header: "User",
				sortable: true,
				dataIndex: "USER",
				id: "USER",
				width: 10
			},{
				header: "% CPU",
				sortable: true,
				dataIndex: "CPU",
				id: "CPU",
				width: 10
			},{
				header: "% MEM",
				sortable: true,
				dataIndex: "MEM",
				id: "MEM",
				width: 10
			},{
				header: "Command",
				sortable: true,
				dataIndex: "COMMAND",
				id: "COMMAND"
			},{
				header: "State",
				sortable: true,
				dataIndex: "STAT",
				id: "STAT",
				renderer: function(val) {
					/*
					PROCESS STATE CODES
						   Here are the different values that the s, stat and state output specifiers
						   (header "STAT" or "S") will display to describe the state of a process.
						   D    Uninterruptible sleep (usually IO)
						   R    Running or runnable (on run queue)
						   S    Interruptible sleep (waiting for an event to complete)
						   T    Stopped, either by a job control signal or because it is being traced.
						   W    paging (not valid since the 2.6.xx kernel)
						   X    dead (should never be seen)
						   Z    Defunct ("zombie") process, terminated but not reaped by its parent.
					*/
					switch(val.substr(0,1)) {
						case 'D': return 'Wait';
						case 'R': return 'Running';
						case 'S': return 'Sleeping';
						case 'T': return 'Paused';
						case 'Z': return 'Zombie';
					}
					return val;
				},
				width: 20
			}]
		})
	};
	Ext.apply(initialConfig, config);
	OMV.Module.Diagnostics.ProcessList.superclass.constructor.call(
			this, initialConfig);
};

Ext.extend(OMV.Module.Diagnostics.ProcessList, OMV.grid.TBarGridPanel, {

	initComponent : function() {
		this.store = new OMV.data.Store({
			autoLoad: false,
			remoteSort: false,
			proxy: new OMV.data.DataProxy("processlist", "getProcessList"),
			sortInfo: {
				field: "CPU", direction: "DESC"
			},
			listeners: {
				load: function(s) {
					s.filter('COMMAND',new RegExp(Ext.getCmp('OMV.Module.Diagnostics.ProcessList-commandfilter').getValue()),false);
				}
			},				
			reader: new Ext.data.JsonReader({
				idProperty: "PID",
				totalProperty: "total",
				root: "data",
				fields: [
				         { name: "PID" },
				         { name: "USER" },
						 { name: "STAT" },
						 { name: "CPU" },
						 { name: "MEM" },
						 { name: "COMMAND" }
				         ]
			})
		});
		OMV.Module.Diagnostics.ProcessList.superclass.initComponent.
		apply(this, arguments);
	},

	listeners : {
		show: function() {
			this.doLoad();
		}
	},

	initToolbar : function() {
		var tbar = OMV.Module.Diagnostics.ProcessList.superclass.initToolbar.apply(this);
		tbar.insert(20,{xtype:'tbseparator'});
		tbar.insert(21,{xtype:'tbspacer', width: 20});
		tbar.insert(22, {
			html: 'Filter:&nbsp;&nbsp;'
		});
		tbar.insert(23, {
			xtype: 'textfield',
			id: 'OMV.Module.Diagnostics.ProcessList-commandfilter',
            listeners: {
                specialkey: function(field, e){
                    if (e.getKey() == e.ENTER) {
						Ext.getCmp('OMV.Module.Diagnostics.ProcessList').getStore().filter('COMMAND',new RegExp(field.getValue()),false);
                    }
                }
            }
		});
		return tbar;
	}


});

// Set standard process list title
OMV.NavigationPanelMgr.getMenu("diagnostics", "processes").items.items[0].title = "Top";

// Register our panel
OMV.NavigationPanelMgr.registerPanel("diagnostics", "processes", {
	cls: OMV.Module.Diagnostics.ProcessList,
	title: "Process List",
	position: 100
});
